<?php 
defined('BASEPATH') OR exit('No direct script access allowed');
session_start();
class MY_Controller extends CI_Controller
{
	function  __construct()
	{
		parent::__construct();
		$this ->load ->database();
		$this ->load ->helper('url');
		$admin_name=$_SESSION['admin_name'];
		$admin_id=$_SESSION['admin_id'];
		if (!$admin_name || !$admin_id) {
			redirect('admin/Login/index');
		}
		if ($admin_id!=1 &&$admin_name!='tao') {
			$this ->jiedian();
		}
	}
	

	// 验证是否有用户名session和用户id
	public function jiedian()
	{
		
		// 获取当前url 也就是模块控制器和方法
		$url1=$this ->uri ->segment(1);
		$url2=$this ->uri ->segment(2);
		$url3=$this ->uri ->segment(3);
		$url=$url1.'/'.$url2.'/'.$url3;
		// 设置所有管理员都有的权限 节点
		$data_url=[
		'admin/admin/index',
		'admin/admin/indexTop',
		'admin/admin/indexLeft',
		'admin/admin/indexDefault',
		];
		// echo(in_array($url,$data_url));die();
		if (!in_array($url,$data_url)) {
			// echo "string";die;
			$admin_name=$_SESSION['admin_name'];
			$admin_id=$_SESSION['admin_id'];
			
			$data=$this->db->select('ac.access_route')
			->from('admin as a')
			->join('admin_role as a_r','a.admin_id=a_r.admin_id')
			->join('role as r','r.role_id=a_r.role_id')
			->join('role_access as r_a','r_a.role_id=r.role_id')
			->join('access as ac','ac.access_id=r_a.access_id')
			->where(['a.admin_id'=>$admin_id])
			->get()
			->result_array();
			// 降维

			if ($data==false) {
				echo "权限不足";die;
				
			}
			foreach ($data as $k => $v) {
				
				//用implode方法,将一维数组转换为用逗号连接的字符串
				$arr[]=$v['access_route'];
				$str=implode(',',$arr);
				//去掉重复的字符串,也就是重复的一维数组,重新生成一个数组
				$data_str=array_unique($arr);
			}
			if (!in_array($url, $data_str)) {
				echo "权限不足";die;
			}
		}

		
		// print_r($data);
		
		// $url1=$this ->uri ->segment(1);
		// $url2=$this ->uri ->segment(2);
		// $url3=$this ->uri ->segment(3);
		// echo $url1;
		// echo $url2;
		// echo $url3;
		// foreach ($data as $k => $v) {
			
		// }



		// $this ->load ->model('admin');
		// $arr=$this ->admin ->
		// $this->load->view('admin/index');
	}

}
 

 ?>